package com.apkfuns.logutils.harmony.slice;

import com.apkfuns.logutils.LogUtils;
import com.apkfuns.logutils.harmony.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Button logVerboseBtn = (Button) findComponentById(ResourceTable.Id_log_verbose_btn);
        Button logInfoBtn = (Button) findComponentById(ResourceTable.Id_log_info_btn);
        Button logDebugBtn = (Button) findComponentById(ResourceTable.Id_log_debug_btn);
        Button logErrorBtn = (Button) findComponentById(ResourceTable.Id_log_error_btn);
        Button logWarnBtn = (Button) findComponentById(ResourceTable.Id_log_warn_btn);
        logVerboseBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handleClick(component, "verbose log !");
            }
        });
        logInfoBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handleClick(component, "info log !");
            }
        });
        logDebugBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handleClick(component, "debug log !");
            }
        });
        logErrorBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handleClick(component, "error log !");
            }
        });
        logWarnBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handleClick(component, "warn log !");
            }
        });
    }

    private void handleClick(Component button, String msg) {
        if (msg == null) {
            return;
        }
        int viewId = button.getId();
        if (ResourceTable.Id_log_verbose_btn == viewId) {
            LogUtils.v(msg);
        } else if (ResourceTable.Id_log_info_btn == viewId) {
            LogUtils.i(msg);
        } else if (ResourceTable.Id_log_debug_btn == viewId) {
            LogUtils.d(msg);
        } else if (ResourceTable.Id_log_error_btn == viewId) {
            LogUtils.e(msg);
        } else if (ResourceTable.Id_log_warn_btn == viewId) {
            LogUtils.w(msg);
        }
        toast(msg);
    }

    private void toast(String msg) {
        ToastDialog toast = new ToastDialog(getContext().getApplicationContext());
        toast.setText(msg);
        toast.setDuration(1000);
        toast.show();
    }
}